<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class Goods extends Model
{
    protected $table = 'goods';

    public function materialList()
    {
        return $this->hasMany('App\Model\MaterialList','goods_id');
    }

    /**
     * 入库出库归还更新物资分类表
     * type:入库类型1:新增入库；3：归还入库;4:出库
     * @param $goods_id
     * @param $material_id
     * @param $checkcategory_id
     * @param $company_id
     * @return void
     */
    public static function operate($goods_id=0){
        $data = Stock::select(DB::raw('SUM(`income_warehouse`) as income_warehouse,SUM(`leave_warehouse`) as leave_warehouse,SUM(`loss_warehouse`) as loss_warehouse,SUM(`warehouse`) as warehouse'))->where('goods_id',$goods_id);
        $query = $data->first();

        $update_order['income_warehouse']=$query['income_warehouse'];
        $update_order['leave_warehouse']=$query['leave_warehouse'];
        $update_order['loss_warehouse']=$query['loss_warehouse'];
        $update_order['warehouse']=$query['warehouse'];

        $res = self::where('id', $goods_id)->update($update_order);
        return $res;
    }

    public function child()
    {
        return $this->hasMany(__CLASS__, 'pid');
    }

    public function children()
    {
        return $this->child()->with('children')->orderBy("id");
    }

    /**
     * 去除children的空数组
     * @param $arr
     * @return mixed
     */
    public static function removeEmptyValues(&$arr){
        // 递归处理每一层子数组
        foreach($arr as $key => &$value){
            if(is_array($value['children'])){
                // 进行递归
                self::removeEmptyValues($value['children']);
                // 如果子数组处理后为空，则删除该元素
                if(empty($value['children'])){
                    unset($arr[$key]['children']);
                }
            }else{
                // 如果当前元素为空，则删除该元素
                if(empty($value['children'])){
                    unset($arr[$key]['children']);
                }
            }
        }
        return $arr;
    }

    /**
     * 通过pid获取id
     * @param $pid
     * @param $arr
     * @return mixed
     */
    public static function getPidByid($pid,&$arr){
        $data = self::where('id',$pid)->first();
        array_push($arr, $data['id']);
        if($data['pid']==0){
            return $arr;
        }else{
            self::getPidByid($data['pid'],$arr);
            return $arr;
            //$data = self::where('id',$data['pid'])->first();
            //return $pid;
        }
    }

    /**
     * 通过id获取pid
     * @param $pid
     * @param $arr
     * @return mixed
     */
    public static function getIdByid($pid,&$arr){
        $data = self::where('pid',$pid)->first();
        array_push($arr, $data['id']);
        if($data['laravel']==4){
            return $arr;
        }else{
            self::getIdByid($data['id'],$arr);
            return $arr;
            //$data = self::where('id',$data['pid'])->first();
            //return $pid;
        }
    }
    /**
     * 通过name值获取相应的id
     * @param $arr
     * @return mixed
     */
    public static function getPidByName(&$name){
        $arr = [];
        // 递归处理每一层子数组
        $getgoods = self::where('name',$name)->first();
        if($getgoods){
            $getgoods = $getgoods->toArray();
            $arr[] = $getgoods['id'];
            $getPidByid[] = $getgoods['pid']?$getgoods['pid']:$getgoods['id'];
            if($getgoods['pid']){
                $getPidByid = self::getPidByid($getgoods['pid'],$arr);
            }/*else{
                $getPidByid = self::getIdByid($getgoods['id'],$getPidByid);
            }*/
        }
        return $getPidByid;
    }

    /**
     * 去除不相关的值
     * @param $arr
     * @return mixed
     */
    public static function removeValues($arr,$val,$i=1){
        // 递归处理每一层子数组
        if($arr){
            //$i=1;
            $level = count($val);
             foreach($arr as $key => $value){
                if(in_array($value['id'],$val)){
                    //self::removeValues($value['children'],$val,$i);
                    if($level>=2){
                        foreach($value['children'] as $ckey => $cvalue){
                            if(!in_array($cvalue['id'],$val)){
                                unset($arr[$key]['children'][$ckey]);

                            }
                            if($level>=3 && isset($cvalue['children'])){
                                foreach($cvalue['children'] as $cckey => $ccvalue){
                                    if(!in_array($ccvalue['id'],$val)){
                                        unset($arr[$key]['children'][$ckey]['children'][$cckey]);
                                    }
                                    if($level==4 && isset($ccvalue['children'])){
                                        foreach($ccvalue['children'] as $ccckey => $cccvalue){
                                            if(!in_array($cccvalue['id'],$val)){
                                                unset($arr[$key]['children'][$ckey]['children'][$cckey]['children'][$ccckey]);
                                            }
                                        }
                                    }

                                }
                            }

                        }
                    }
                    /*if(count($val)!=$i){
                        // 进行递归
                        self::removeValues($value['children'],$val,$i);
                        foreach($value['children'] as $ckey => &$cvalue){
                            if(!in_array($cvalue['id'],$val)){
                                unset($arr[$key]['children']);
                            }
                        }
                    }*//*else{
                        if(empty($value['children'])){
                            unset($arr[$key]['children']);
                        }
                    }*//*else{
                        if(count($val)==$i){
                            // 进行递归
                            self::removeValues($value['children'],$val);
                        }
                    }*/
                    //var_dump($i);
                    $i++;

                }else{
                    unset($arr[$key]);
                    // 如果当前元素为空，则删除该元素

                }
            }
        }
        return $arr;
    }

    /**
     * 递归
     * @param $data
     * @param $pk
     * @param $pid
     * @param $child
     * @param $root
     */
    public static function sort_data($data, $pk = 'value', $pid = 'pid', $child = 'children', $root = 0)
    {
        // 创建Tree
        $tree = [];
        if (!is_array($data)) {
            return false;
        }
        //$user = Auth::user();
        //创建基于主键的数组引用
        $refer = [];
        foreach ($data as $key => $value_data) {
            $refer[$value_data[$pk]] = &$data[$key];
        }
        foreach ($data as $key => $value_data) {
            // 判断是否存在parent
            $parentId = $value_data[$pid];
            if ($root == $parentId) {
                $tree[] = &$data[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent = &$refer[$parentId];
                    $parent[$child][] = &$data[$key];
                }
            }
        }
        return $tree;
    }
}
